package com.zmht.beike.model;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class Pagination extends TagSupport{

	/**
	 * @author 
	 * 
     * 创建日期�?2009-8-14
     * 
     * 说明：分页标签，自动生成html
     * 
     * 参数及返回�?�说明：
     * 
     * @param url			链接url
     * @param type			提交方式，默认get(暂未实现)，post时form为必填项
     * @param data			参数传�??
     * @param form			form的id
     * @param currPage		当前页码
     * @param pageSize		每页显示条数
     * @param pageAmount	记录总页�?
     * @param imgPath		图片(按钮)路径
     * 
     * 用法与示例：
     * 
     * 在html页中引入<%@ taglib uri="/WEB-INF/tld/pagination.tld" prefix="page"%>�?
     * tld文件路径根据自己存放位置更改，在要显示分页的位置加入如下代码�?
     * <page:Pagination url="/insuranceCompany.do?method=getAllInsuranceCompany" currPage="2" pageAmount="5" pageSize="10" type="post" form="form1" />
	 */
	private static final long serialVersionUID = 1L;
	private String url="";//url
	private String type="get";//提交方式，默认get(暂未实现)，post时form为必填项
	private String data="";//参数
	private String form="";//form的id
	private String currPage="1";//当前页码
	private String pageSize="10";//每页条数
	private String pageAmount="1";//总页�
	private String imgPath="/SpecialSchool/reference/images/button_go.gif";//input图片(按钮)
	
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			out.println(getPageHTML());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
	
	private String getPageHTML(){
		StringBuffer PageOut = new StringBuffer();
		PageOut.append("<div><table><tr>");
		//JavaScript验证
		PageOut.append("<script>");
		//验证当前页与每页条数为正整数
		PageOut.append("function checkPage(pageInfo){");
		PageOut.append("var regu =  /^\\d+$/;");//  /^\\d+$/   ^\+?[1-9][0-9]*$  /^\d+$/;
		PageOut.append("return regu.test(pageInfo);");
		PageOut.append("}");
		//根据点击的链接还是图片设置action提交路径
		PageOut.append("function subForm(pageNo){");
		PageOut.append("if(pageNo==''){");
		PageOut.append("document."+form+".action='"+url+"';");
		PageOut.append("}else{");
		PageOut.append("document."+form+".action='"+url+"&currPage='+pageNo;");
		PageOut.append("}document."+form+".submit();");
		PageOut.append("}");
		PageOut.append("</script>");
		//页码Html代码拼接
		PageOut.append("<td><FONT color=#8a8a8a>"+currPage+"/"+pageAmount+"页</FONT>&nbsp;");
		PageOut.append("<input type='hidden' name='pageAmount' id='pageAmount' value='"+pageAmount+"'>");
		if(Integer.parseInt(currPage)==1&&Integer.parseInt(pageAmount)==1){//只有�?页信息时，链接均不可�?
			PageOut.append("<FONT color=#8a8a8a>首页&nbsp;前页&nbsp;后页&nbsp;尾页&nbsp;</FONT>");
		}
		else if(Integer.parseInt(currPage)==1&&Integer.parseInt(pageAmount)>1){//第一页时，首页与前页不可�?
			PageOut.append("<FONT color=#8a8a8a>首页&nbsp;前页&nbsp;</FONT>");
			PageOut.append("<a href=javascript:subForm('"+(Integer.parseInt(currPage)+1)+"')>后页</a>&nbsp;");
			PageOut.append("<a href=javascript:subForm('"+pageAmount+"')>尾页</a>&nbsp;");
		}
		else if(Integer.parseInt(currPage)<Integer.parseInt(pageAmount)){//多页信息并且当前页为中间页时，均可用
			PageOut.append("<a href=javascript:subForm('1')>首页</a>&nbsp;");
			PageOut.append("<a href=javascript:subForm('"+(Integer.parseInt(currPage)-1)+"')>前页</a>&nbsp;");
			PageOut.append("<a href=javascript:subForm('"+(Integer.parseInt(currPage)+1)+"')>后页</a>&nbsp;");
			PageOut.append("<a href=javascript:subForm('"+pageAmount+"')>尾页</a>&nbsp;");
		}
		else{//末页时，后页与尾页不可用
			PageOut.append("<a href=javascript:subForm('1')>首页</a>&nbsp;");
			PageOut.append("<a href=javascript:subForm('"+(Integer.parseInt(currPage)-1)+"')>前页</a>&nbsp;");
			PageOut.append("<FONT color=#8a8a8a>后页&nbsp;</FONT>");
			PageOut.append("<FONT color=#8a8a8a>尾页&nbsp;</FONT>");
		}
		//当前页码设置(含验�?)
		PageOut.append("第<INPUT class=s8 maxLength=3 size=2 name='currPage' id='currPage' value="+currPage+" onblur='if(!checkPage(this.value)||this.value==0){alert(\"当前页码必须为正整数！\");this.value=\""+currPage+"\";};if(this.value>"+pageAmount+"){alert(\"页码数不能大于�总页数！\");this.value=\""+currPage+"\";};'>页&nbsp;");
		//每页条数设置(含验�?)
		PageOut.append("每页<INPUT class=s8 maxLength=3 size=3 name='pageSize' id='pageSize' value='"+pageSize+"' onblur='if(!checkPage(this.value)){alert(\"每页显示条数必须为正整数！\");this.value=\""+pageSize+"\";};'>条&nbsp;");
		PageOut.append("</td><td><IMG height=16 src='"+imgPath+"' style='cursor:hand' width=18 name=pageJump id=pageJump type='submit' onclick='subForm(\"\");'></td>");
		PageOut.append("</tr></table></div>");		
		return PageOut.toString();
	}

	public String getCurrPage() {
		return currPage;
	}

	public void setCurrPage(String currPage) {
		if(Integer.parseInt(currPage)<1){
			currPage = "1";
		}
		this.currPage = currPage;
	}

	public String getImgPath() {
		return imgPath;
	}

	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getForm() {
		return form;
	}

	public void setForm(String form) {
		this.form = form;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		url +="&pageUrl=1";
		this.url = url;
	}

	public String getPageAmount() {
		return pageAmount;
	}

	public void setPageAmount(String pageAmount) {
		if(Integer.parseInt(pageAmount)<1){
			pageAmount = "1";
		}
		this.pageAmount = pageAmount;
	}
}
